<!--
  * 暂收送检单
  *
  * @Author:    AKITA
  * @Date:      2025-07-24 14:41:19
  * @Copyright  AKITA
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="电键" class="smart-query-form-item">
        <a-input style="width: 200px" v-model:value="queryForm.id" placeholder="电键" />
      </a-form-item>
      <a-form-item class="smart-query-form-item">
        <a-button type="primary" @click="onSearch">
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card :bordered="false" >
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-operate-block">

      </div>
      <div class="smart-table-setting-block">
        <TableOperator v-model="columns" :tableId="null" :refresh="queryData" />
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格 begin ----------->
    <a-table width="100%" :dataSource="tableData" :columns="columns" rowKey="id" :loading="tableLoading" :pagination="false" :scroll="{ x: 600 }">
      <template #bodyCell="{ text, record, column, index }">
        <template v-if="column.dataIndex === 'id'">
          <a-button type="link" @click="showForm(record)">{{ text }}</a-button>
        </template>
      </template>
    </a-table>
    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageNum"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>
</template>
<script setup>
  import { reactive, ref, onMounted } from 'vue';
  import { message, Modal } from 'ant-design-vue';
  import { SmartLoading } from '/src/components/framework/smart-loading';
  import { subordbackApi } from '/src/api/business/erp/subordback-api.js';
  import { PAGE_SIZE_OPTIONS } from '/src/constants/common-const';
  import { smartSentry } from '/src/lib/smart-sentry';
  import TableOperator from '/src/components/support/table-operator/index.vue';
  import { useRouter } from 'vue-router';

  const router = useRouter();

  const doc = reactive({
    tm: 'subordback',
    tda: 'subordaback',
  });

  // ---------------------------- 表格列 ----------------------------

  const columns = ref([
    {
      title: '电键',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '对象名',
      dataIndex: 'objName',
      ellipsis: true,
    },
    {
      title: '单据状态',
      dataIndex: 'docStatus',
      ellipsis: true,
    },
    {
      title: '月期间',
      dataIndex: 'absId',
      ellipsis: true,
    },
    {
      title: '审核',
      dataIndex: 'approved',
      ellipsis: true,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      ellipsis: true,
    },
    {
      title: '版本号',
      dataIndex: 'version',
      ellipsis: true,
    },
    {
      title: '附件名称',
      dataIndex: 'attachment',
      ellipsis: true,
    },
    {
      title: '创建者',
      dataIndex: 'createBy',
      ellipsis: true,
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      ellipsis: true,
    },
    {
      title: '部门编码',
      dataIndex: 'deptId',
      ellipsis: true,
    },
    {
      title: '部门名称',
      dataIndex: 'deptName',
      ellipsis: true,
    },
    {
      title: '供应商编码',
      dataIndex: 'vendId',
      ellipsis: true,
    },
    {
      title: '供应商名称',
      dataIndex: 'vendName',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 90,
    },
  ]);

  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    id: undefined, //电键
    pageNum: 1,
    pageSize: 10,
  };
  // 查询表单form
  const queryForm = reactive({ ...queryFormState });
  // 表格加载loading
  const tableLoading = ref(false);
  // 表格数据
  const tableData = ref([]);
  // 总数
  const total = ref(0);

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize;
    Object.assign(queryForm, queryFormState);
    queryForm.pageSize = pageSize;
    queryData();
  }

  // 搜索
  function onSearch() {
    queryForm.pageNum = 1;
    queryData();
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true;
    try {
      let queryResult = await subordbackApi.queryPage(queryForm);
      tableData.value = queryResult.data.list;
      total.value = queryResult.data.total;
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  onMounted(queryData);


  function showForm(data) {
    router.push({ path: doc.tda, query: { type: 'info', id: data.id } });
  }
</script>
